﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Model;
using System.Windows.Data;
using VAdvantage.Controls;

namespace VAdvantage.Grids
{
    public class VDataGridComboBoxColumn : DataGridBoundColumn, IDataGridColumn, ICustomSortColumn
    {
        private Lookup lookup = null;
        private GridField mField = null;
        private VComboBox editingControl = null;
        private Binding binding = null;
        bool isNumber = false;
        ComboValueToText converter = null;
        string _columnName = "";
        private Object currentCellValue = null;



        public VDataGridComboBoxColumn()
        {
        }

        private void SetDefaultTemplates()
        {

        }



        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            if (cell.Content is VComboBox)
            {
                editingControl.VetoableChangeListener -= new EventHandler(editingControl_VetoableChangeListener);
                
                currentCellValue = ((VComboBox)cell.Content).SelectedValue;
                mField.SetValue(currentCellValue);
                ((DataUtil.DataObject)dataItem).SetFieldValue(_columnName, currentCellValue);
                return currentBlock;
                //currentBlock.Text = value == null ? "0" : value.ToString();
            }


            TextBlock text = Utility.AEnv.GetDataGridElement();
            if (binding != null)
            {
                text.SetBinding(TextBlock.TextProperty, binding);
            }
            //return text;

            
            return text;

        }

        TextBlock currentBlock = null;
        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            if (editingControl == null)
            {
                editingControl = new VComboBox(mField.GetColumnName(), mField.IsMandatory(false), mField.IsReadOnly(), mField.IsEditable(false), mField.GetLookup(), mField.GetDisplayLength());
                editingControl.SetField(mField);
                editingControl.IsEditingControl = true;
            }
            //editingControl.SelectedValue = cell.

            currentCellValue = ((DataUtil.DataObject)dataItem).GetFieldValue(_columnName);
            editingControl.SelectedValue = currentCellValue;

            if (cell.Content is TextBlock)
            {
                currentBlock = (TextBlock)cell.Content;
            }

            return editingControl;
        }

        void editingControl_VetoableChangeListener(object sender, EventArgs e)
        {
            DataGridView.RaiseCellValueChanged(sender);
            editingControl.VetoableChangeListener -= new EventHandler(editingControl_VetoableChangeListener);
        }


        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            
            editingControl.VetoableChangeListener += new EventHandler(editingControl_VetoableChangeListener);
            return editingElement;
        }


        public GridField Field
        {
            get
            {
                return mField;
            }
            set
            {
                mField = value;
                if (mField != null)
                {
                    //editingControl = new VComboBox(mField.GetColumnName(), mField.IsMandatory(false), mField.IsReadOnly(), mField.IsEditable(false), mField.GetLookup(), mField.GetDisplayLength());
                    //editingControl.SetField(mField);
                    //editingControl.IsEditingControl = true;
                    isNumber =  mField.GetColumnName().EndsWith("_ID");
                    lookup = mField.GetLookup();
                }
            }
        }

        private void SetBinding()
        {
            binding = new Binding(_columnName);
            binding.Mode = BindingMode.OneWay;
            converter = new ComboValueToText();
            if (lookup != null)
            {
                converter.DataSource = lookup;
            }
            binding.Converter = converter;
        }


        public override Binding Binding
        {
            get
            {
                return binding;
            }
            set
            {
                base.Binding = binding;
            }
        }


        public void Dispose()
        {
            lookup = null;
             mField = null;
            if (editingControl != null)
            {
                editingControl.VetoableChangeListener -= new EventHandler(editingControl_VetoableChangeListener);
                editingControl.Dispose();
            }
            editingControl = null;
            binding = null;
            if (converter != null)
            {
                converter.Dispose();
            }
            currentCellValue = null;
            currentBlock = null;
        }

        public string DataPropertyName
        {
            get
            {
                return _columnName;
            }
            set
            {
                _columnName = value.ToUpper();
                SetBinding();
            }
        }


        public VDataGrid DataGridView
        {
            get
            ;
            set
            ;
        }


        public bool ReadOnly
        {

            get;
            set;

        }
    }
}
